import 'package:bloc/bloc.dart';
import 'package:logger/logger.dart';
import 'package:qgchat/http/apis/common.dart';
import 'package:qgchat/http/models/mysenddongtaiModel.dart';

enum MyDynamicEvent { refresh, load }

class MyDynamicBloc extends Bloc<MyDynamicEvent, List<MysenddongtaiModel>> {
  @override
  List<MysenddongtaiModel> get initialState => [];

  @override
  Stream<List<MysenddongtaiModel>> mapEventToState(MyDynamicEvent event) async* {
    switch (event) {
      // 刷新列表
      case MyDynamicEvent.refresh:
        yield* _refresh();
        break;
      // 加载列表
      case MyDynamicEvent.load:
        yield* _load();
        break;
    }
  }

  int _page = 1; // 分页
  List<MysenddongtaiModel> _list = []; // 列表

  /// 刷新列表
  Stream<List<MysenddongtaiModel>> _refresh() async* {
    _page = 1;
    _list = await _getlist();
    yield List<MysenddongtaiModel>.from(_list);
  }

  /// 加载列表
  Stream<List<MysenddongtaiModel>> _load() async* {
    _page = ++_page;
    List<MysenddongtaiModel> list = await _getlist();
    _list.addAll(list);
    yield List<MysenddongtaiModel>.from(_list);
  }

  /// 获取列表
  Future<List<MysenddongtaiModel>> _getlist() async {
    return await CommonApis.mysenddongtai(page: _page);
  }

}